package com.example.demo.controller;



import com.example.demo.Service.ReaderService;
import com.example.demo.model.Book;
import com.example.demo.model.Borrow;
import com.example.demo.model.Librarian;
import com.example.demo.model.Reader;
import com.example.demo.mapper.ReaderMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/reader")
@RestController
public class ReaderController {


    @Autowired
    ReaderService readerService;


     //查询读者信息
//    @PostMapping("reader")
//    public JsonData findReader(@RequestParam("reader_id")Integer reader_id){
//        Reader reader=readerService.findReader(reader_id);
//        if(reader != null){
//
//            return JsonData.buildSuccess(reader);
//        }else {
//            return JsonData.buildError("查询失败");
//        }
//    }

    //修改读者信息
    @PostMapping("modify")
    public JsonData modifyReader(@RequestBody Reader reader){
        Integer result=readerService.modifyReader(reader);
        if(result != 0){

            return JsonData.buildSuccess(result);
        }else {
            return JsonData.buildError("修改失败");
        }
    }
     //查询书籍信息
//     @PostMapping("querybook")
//     public JsonData queryBook(@RequestParam("book_name")String book_name){
//         Book book=readerService.queryBook(book_name);
//         if(book != null){
//
//             return JsonData.buildSuccess(book);
//         }else {
//             return JsonData.buildError("查询失败");
//         }
//     }
//    public Reader FindReader(@RequestParam("reader_id") Integer reader_id ){
//        System.out.println("reader_id"+reader_id);
//        Reader reader=readerService.findReader(reader_id);
//        if(reader!=null){
//            System.out.println(reader.toString());
//            return reader;
//        }
//
//        return null;
//
//    }

    @PostMapping("queryborrow")
    public JsonData queryBorrow(@RequestParam("reader_id" )Integer reader_id){
        System.out.println(reader_id);
        List<Borrow> borrow=readerService.queryBorrow(reader_id);
        if(borrow != null){

            return JsonData.buildSuccess(borrow);
        }else {
            return JsonData.buildError("查询失败");
        }
    }
    @GetMapping("list")
    public JsonData getLibrarianList(){
        List<Reader> result = readerService.getReaderList();
        if(result != null){
            return JsonData.buildSuccess(result);
        }else {
            return JsonData.buildError("查询失败");
        }
    }

    @PostMapping("add")
    public JsonData addReader(@RequestBody Reader newReader){
        Integer result = readerService.addReader(newReader) ;
        if(result != null){

            return JsonData.buildSuccess(result);
        }else {
            return JsonData.buildError("插入失败");
        }
    }


    @PostMapping("delete")
    public JsonData deleteReader(@RequestBody Map<String, Integer> user){
        Integer result = readerService.deleteReader(user.get("reader_id"));
        if(result != 0){
            return JsonData.buildSuccess(result);
        }else {
            return JsonData.buildError("删除失败");
        }
    }

}
